//
//  XS_DownloadStartBottomView.swift
//  XueDao
//
//  Created by Mac on 2021/1/9.
//

import UIKit

enum XS_DownloadStartBottomViewType {
    case download
    case manage
}

protocol XS_DownloadStartBottomViewDelegate {
    func bottomView(bottomView : XS_DownloadStartBottomView, didClickAllBtn:QMUIButton)
    func bottomView(bottomView : XS_DownloadStartBottomView, didClickOperateBtn:UIButton)
}

class XS_DownloadStartBottomView: UIView {
    
    var _type : XS_DownloadStartBottomViewType!
    public var type : XS_DownloadStartBottomViewType!{
        set{
            _type = newValue
            //设置数据
            allBtn.setTitle("已选择0节", for: .normal)
            if newValue == XS_DownloadStartBottomViewType.download {
                operateBtn.setTitle("下载", for: .normal)
            }else{
                operateBtn.setTitle("删除", for: .normal)
            }
        }
        get{
            return _type
        }
    }
    
    var delegate : XS_DownloadStartBottomViewDelegate?
    
    lazy var allBtn : QMUIButton = {
        let allBtn = QMUIButton.init(frame: .zero)
        allBtn.imagePosition = .left
        allBtn.spacingBetweenImageAndTitle = 8
        allBtn.setImage(#imageLiteral(resourceName: "选择_默认"), for: .normal)
        allBtn.setImage(UIImage.init(named: "选择_已选"), for: .selected)
        allBtn.titleLabel?.font = .systemFont(ofSize: 14)
        allBtn.setTitleColor(#colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), for: .normal)
        allBtn.addTarget(self, action: #selector(allBtnAction(_:)), for: .touchUpInside)
        return allBtn
    }()
    lazy var operateBtn : UIButton = {
        let operateBtn = UIButton.init(frame: .zero)
        operateBtn.backgroundColor = #colorLiteral(red: 0.1716121137, green: 0.7779735923, blue: 0.675088048, alpha: 1)
        operateBtn.layer.cornerRadius = 20
        operateBtn.clipsToBounds = true
        operateBtn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        operateBtn.setTitleColor(.white, for: .normal)
        operateBtn.addTarget(self, action: #selector(operateBtnAction(_:)), for: .touchUpInside)
        return operateBtn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(allBtn)
        addSubview(operateBtn)
        
        allBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview()
            make.height.equalTo(49)
        }
        
        operateBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 98, height: 40))
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(4.5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func allBtnAction(_ sender: QMUIButton) {
        sender.isSelected = !sender.isSelected
        if delegate != nil {
            delegate?.bottomView(bottomView: self, didClickAllBtn: sender)
        }
    }
    
    @objc func operateBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.bottomView(bottomView: self, didClickOperateBtn: sender)
        }
    }
}
